package com.sun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.model.entity.UserInfo;
import com.sun.preproces.UserPreprocessing;
import com.sun.service.user.UserInfoService;
import com.until.controller.AbsController;
import com.until.pagination.PaginationRequest;
import com.until.request.ParamNotNull;
import com.until.response.AbsResponseBody;
import com.until.response.RestfulCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author AdminUser
 */
@RestController
@RequestMapping("/user")
public class UserInfoController  extends AbsController<UserInfo, RestfulCode.CodeSatCode, Integer, UserPreprocessing> {
    @Autowired
    private UserInfoService userInfoService;

    @ModelAttribute
    public UserPreprocessing init(@ModelAttribute PaginationRequest paginationRequest, @ModelAttribute UserInfo userInfo,
                                  @RequestParam(value = "action",required = false,defaultValue = "")String action, HttpSession session) {
        UserPreprocessing preprocessing = new UserPreprocessing();
        preprocessing.setPaginationRequest(ParamNotNull.objCheckIsNull(paginationRequest)?new PaginationRequest(1,5):paginationRequest);
        preprocessing.setUserInfo(ParamNotNull.objCheckIsNull(userInfo)?new UserInfo():userInfo);
        preprocessing.setAction(action);
        return preprocessing;
    }

    @GetMapping("")
    @Override
    @ResponseBody
    public RestfulCode.CodeSatCode all(@ModelAttribute UserPreprocessing page) {
        PageHelper.startPage(page.getPaginationRequest().getPageNumber(), page.getPaginationRequest().getPageSize());
        try {
            // todo 根据请求动作得到结果
            return RestfulCode.getSuccessResult(new PageInfo<>(userInfoService.coreSelection(page.getUserInfo(), page.getAction(), HttpMethod.GET)));
        } catch (Exception e) {
            return RestfulCode.getSuccessResult(new PageInfo<>(userInfoService.selectByExample(null)));
        }
    }
    
    @PostMapping("")
    @Override
    public RestfulCode.CodeSatCode add(UserPreprocessing page) {
        int successfully = userInfoService.insertSelective(page.getUserInfo());
        System.out.println(successfully);
        RestfulCode.CodeSatCode codeSatCode = successfully > 0 ? AbsResponseBody.USER_OPERATION_SUCCEEDED : AbsResponseBody.USER_ACTION_FAILED;
        return RestfulCode.getFreeResult(codeSatCode.getFlagCode(), codeSatCode.getMessage(),null);
    }

    @GetMapping("/{id}")
    @Override
    @ResponseBody
    public RestfulCode.CodeSatCode get(@PathVariable Integer id) {
        UserInfo userInfo=userInfoService.selectByPrimaryKey(id);
        RestfulCode.CodeSatCode codeSatCode = ParamNotNull.objCheckIsNull(userInfo) ? AbsResponseBody.NOT_FOUND_EXAMPLE : AbsResponseBody.USER_ACTION_FAILED;
        return RestfulCode.getFreeResult(codeSatCode.getFlagCode(), codeSatCode.getMessage(),userInfo);
    }

    @PostMapping("/userLogin.action")
    @ResponseBody
    public RestfulCode.CodeSatCode userLogin(@ModelAttribute UserPreprocessing page) throws Exception {
        System.out.println("a");
        UserInfo userInfo = userInfoService.coreSelection(page.getUserInfo(), "UserLogin", HttpMethod.POST).get(0);
        RestfulCode.CodeSatCode codeSatCode = !ParamNotNull.objCheckIsNull(userInfo) ? AbsResponseBody.USER_OPERATION_SUCCEEDED : AbsResponseBody.USER_ACTION_FAILED;
        return RestfulCode.getFreeResult(codeSatCode.getFlagCode(), codeSatCode.getMessage(),userInfo);
    }

    @ExceptionHandler
    @Override
    public RestfulCode.CodeSatCode handleException(Exception ex) {
        ex.printStackTrace();
        return RestfulCode.getFreeResult(AbsResponseBody.USER_ACTION_FAILED.getFlagCode(), AbsResponseBody.USER_ACTION_FAILED.getMessage());
    }
}
